<?php

	$errors = "";
	
	function myErrorHandler($errno, $errstr, $errfile, $errline) {
	#	print $errno . "<br>";
		global $errors;
		if ($errno == 2) {
			$errors .= $errstr . "<br>";
		}
		/* Don't execute PHP internal error handler */
		return true;
	}

	$eml = utf8_encode( stripslashes($_REQUEST['eml']) );
	$xmlschema = stripslashes($_REQUEST['schema']);
//'https://code.ecoinformatics.org/code/eml/tags/RELEASE_EML_2_1_0/eml.xsd'	

	if ($eml != "") {
		$xdoc = new DomDocument;
		file_put_contents("eml.xml", $eml);
#		$xmlschema = "https://code.ecoinformatics.org/code/eml/tags/RELEASE_EML_2_1_0/eml.xsd";
		@$xdoc->loadXML( $eml );
		// set to the user defined error handler
		$old_error_handler = set_error_handler("myErrorHandler");
		$res = @$xdoc->schemaValidate($xmlschema);
	} else {
		$errors = "No XML to process.";
	}
?>
<html><style type="text/css">
<!--
body,td,th {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 14px;
}
body {
	margin-left: 10px;
	margin-top: 10px;
}
.errors {
	padding: 5px 5px 5px 15px;
	background-color: #FFE;
	border: #000 solid thin;
	font-size: 12px;
}
-->
</style><title>EML Validator</title>
	<body bgcolor="#efefef" link="#009900" vlink="#009900" alink="#009900">
		
		<h2>EML Validator</h2>
    <?php 
			if ($errors != "") {
				print "<div class='errors'>ERRORS:<br>" . $errors . "</div><br>";
			} else {
				print "<br><b>EML is VALID!!!</b><br>";
			}
		?>
		<form name="form1" method="post" action="">
		    <input type="submit" name="button" id="button" value="Validate EML">
		    &nbsp;&nbsp;&nbsp;&nbsp;
			<input type="reset" name="clear" id="clear" value="Clear XML">
		    <br>
		    <br>
		    Paste your eml xml here:<br>
	    <textarea name="eml" id="eml" cols="120" rows="25"><?php print $eml; ?></textarea>
      <br>
      <br>
      Validation Schema Uri:
      <label>
        <input name="schema" type="text" id="schema" value="http://rs.gbif.org/schema/eml-gbif-profile/dev/eml.xsd" size="100">
      </label>
      <br>
  </form>
		<p>&nbsp;</p>
	</body>
</html>